import 'package:flutter/cupertino.dart';
import 'package:yuan_hang_app/base/base_state.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/utils/common_req_utils.dart';
import 'package:yuan_hang_app/views/home/model/leads_follow_info.dart';
import 'package:yuan_hang_app/widget/common_attachment.dart';

import '../../../../http/api.dart';
import '../../../../http/http_utils.dart';
import '../../../../widget/label_item.dart';
import '../../../../widget/w_toast.dart';

class LeadsFollowUpInfoPage extends StatefulWidget {
  final int followId;

  const LeadsFollowUpInfoPage({super.key, required this.followId});

  @override
  BaseState<LeadsFollowUpInfoPage> createState() => _LeadsFollowUpInfoPageState();

}

class _LeadsFollowUpInfoPageState extends BaseState<LeadsFollowUpInfoPage> {

  LeadsFollowInfo followInfo = LeadsFollowInfo();
  List picList = [];
  List picUrlList = [];
  GlobalKey<CommonAttachmentState>? picKey;


  @override
  void initState() {
    super.initState();
    picKey = GlobalKey(debugLabel: "picKey");

    showLoading();
    HttpUtils().get("${Api.getFollowDetail}/${widget.followId}").then((value) {
      hideLoading();
      if (value.resultCode == 200) {
        if(value.data != null) {
          followInfo = LeadsFollowInfo.fromJson(value.data);
          picList = (followInfo.filesList ?? [])
              .map((item) => item["fileUploadInfoId"])
              .toList();
          picUrlList = (followInfo.filesList ?? [])
              .map((item) => item["url"])
              .toList();
          print("picUrlList  ${picUrlList}");
          if(picUrlList.isNotEmpty) {
            picKey!.currentState?.refreshList(picUrlList);
          }
          setState(() {});
        }
      } else {
        Toast.toast(value.errMsg);
      }
    });
  }

  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        title: "线索跟进详情",
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            LabelItem(label: "跟进方式", value: CommonReqUtils.exchangeCodeToName(CommonReqUtils.followUpWay, followInfo.followMethod)),
            LabelItem(label: "跟进结果", value: CommonReqUtils.exchangeCodeToName(CommonReqUtils.followUpResult, followInfo.followResult)),
            if (followInfo.defeatReason != null)
              LabelItem(label: "战败原因", value: CommonReqUtils.exchangeCodeToName(CommonReqUtils.defeatResult, followInfo.defeatReason)),
            if (followInfo.invalidReason != null)
              LabelItem(label: "无效原因", value: CommonReqUtils.exchangeCodeToName(CommonReqUtils.invalidityReason, followInfo.invalidReason)),
            LabelItem(
              label: "意向级别",
              value: CommonReqUtils.exchangeCodeToName(CommonReqUtils.intentRange, followInfo.level)),
            LabelItem(label: "跟进记录", value: followInfo.followRemark, labelType: LabelType.TEXTAREA, isEnabled: false,),
            LabelItem(label: "下次跟进时间", value: followInfo.nextFollowTime),
            LabelItem(
              label: "邀约到店/试驾",
              value: CommonReqUtils.exchangeCodeToName(CommonReqUtils.appointType, followInfo.isAppointDrive)),
            if(followInfo.isAppointDrive == 10221002)
              LabelItem(label: "预约试驾车型", value: followInfo.modelName),
            if(followInfo.isAppointDrive != null)
              LabelItem(label:followInfo.isAppointDrive == 10221001 ? "预约到店时间" : "预约试驾时间", value: "${followInfo.appointDriveTime} ${followInfo.appointDriveTimeShow}"),
            CommonAttachment(key: picKey, picList: picList, picUrlList: picUrlList, enabled: false,)
          ]
        )
    );
  }

}